package com.hss.questions.stackqueue;

import java.util.Stack;

/**
 用两个栈实现队列 用两个栈来实现一个队列，完成队列的 Push 和 Pop 操作。

 http://www.cyc2018.xyz/%E7%AE%97%E6%B3%95/%E5%89%91%E6%8C%87%20Offer%20%E9%A2%98%E8%A7%A3/9.%20%E7%94%A8%E4%B8%A4%E4%B8%AA%E6%A0%88%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97.html#%E9%A2%98%E7%9B%AE%E9%93%BE%E6%8E%A5

 * */
public class TwoStackImplQueue <T>{

    Stack<T> in = new Stack<>();
    Stack<T> out = new Stack<>();



    public void push(T t){
        in.push(t);

    }

    public T pop(){

        if (out.empty()){
            while (!in.empty()){
                out.push(in.pop());
            }
        }
        if (out.empty()){
            return null;
        }
        T t =out.pop();
        return t;
    }


    public static void main(String[] args) {
        TwoStackImplQueue<Integer> que = new TwoStackImplQueue<>();

        que.push(123);
        que.push(456);
        que.push(789);
        System.out.println(" x = "+ que.pop());
        System.out.println(" x = "+ que.pop());
        que.push(1234);
        System.out.println(" x = "+ que.pop());

    }
}
